<template>
  <div class="worktask-assign-container">
    <!-- <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form> -->
    <el-tabs type="border-card">
      <el-tab-pane label="新合同业务待分派">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="id"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="title"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="title"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="合同业务名称"
            prop="title"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="操作" width="300">
            <template #default="{ row }">
              <el-button type="primary" @click="assignWorkTask(row)">
                审核业务提单并分派任务
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已分派美工任务">已分派美工任务/制作中</el-tab-pane>
      <el-tab-pane label="已分派视频任务">已分派视频任务/制作中</el-tab-pane>
      <el-tab-pane label="已分派开发任务">已分派开发任务/制作中</el-tab-pane>
      <el-tab-pane label="已提报完成待审核">已提报完成待审核</el-tab-pane>
      <el-tab-pane label="审核未通过/已退回">审核未通过/已退回</el-tab-pane>
      <el-tab-pane label="已审核通过">已审核通过</el-tab-pane>
      <el-tab-pane label="无需分派的合同业务">无需分派的合同业务</el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/worktaskAssign'
  import Edit from './components/WorktaskAssignEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'WorktaskAssign',
    components: { Edit },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const assignWorkTask = (row) => {
        router.push({
          path: 'worktaskAssign',
          query: { businessid: row.businessid },
        })
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        assignWorkTask,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
